package com.example.shengyin.frontController;

import com.example.shengyin.entity.Page;
import com.example.shengyin.entity.Subject;
import com.example.shengyin.service.SubjectService;
import com.example.shengyin.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/front/subject")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
@Api(tags = "SubjectFrontController",description = "课程")
public class SubjectFrontController {
    public static final String STAR = "STAR综合";
    public static final String OMP = "OMP";
    public static final String A2 = "A2";
    public static final String A1 = "A1";
    public static final String B = "B";
    public static final String SERVICE = "服务";
    @Autowired
    private SubjectService subjectService;
    @GetMapping("/list")
    @ApiOperation("分类")
    public Result searchByName(){
//        int count1 = subjectService.count2(STAR);
//        int count2 = subjectService.count2(OMP);
//        int count3 = subjectService.count2(A);
//        int count4 = subjectService.count2(B);
//        if (count1==0||count2==0||count3==0||count4==0)
//            return Result.error(null);
        Map<String,Object> m = new HashMap<>();
        m.put("STAR课程",subjectService.list2(STAR));
        m.put("OMP课程",subjectService.list2(OMP));
        List<Subject> s1 = subjectService.list2(A1);
        List<Subject> s2 = subjectService.list2(A2);
        List<Subject> s3 = subjectService.list2(B);
        s2.addAll(s3);
        s1.addAll(s2);
        m.put("A/B课程",s1);
        m.put("家长培训班",subjectService.list2(SERVICE));
        return Result.success(m);
    }
    @GetMapping("/search/{page}/{limit}")
    @ApiOperation("分页查询课程信息")
    public Result search(@PathVariable(value = "page") int page,
                         @PathVariable(value = "limit")int limit){
        Page page1 = new Page(page,limit);
//        int count1 = subjectService.count3(CLASSES);
        List<Subject> list = subjectService.list3(page1);
        if (list==null){
            return Result.success(page1,"没有数据");
        }
        page1.setData(page1);
//        page1.setCount(count1);
        return Result.success(list);
    }
    @GetMapping("/findCname")
    @ApiOperation("查找课程名称")
    public Result findCname(){
        List<String> cname = subjectService.findCname();
        return cname==null?Result.error("无"):Result.success(cname);
    }
    @GetMapping("/show")
    @ApiOperation("根据课程名称查找所有信息")
    public Result page1(@ApiParam(name = "cname",value = "课程名称") @RequestParam(required = false) String cname){
        List<Subject> list = subjectService.page1(cname);
        return list==null?Result.error("无"):Result.success(list);
    }
}
